<template>
  <div class="main">
    <div class="container">
      <h1>欢迎登录疫苗管理系统</h1>
      <el-form ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="account">
          <el-input
            v-model="form.account"
            prefix-icon="el-icon-user"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn1" type="primary" @click="login">登录</el-button>
          <el-button class="btn2" type="primary" @click="res">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.baseURL = "http://localhost:8888";
export default {
  name: "Login",
  data() {
    return {
      form: {},
      rules: {
        account: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    login() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          axios.post(`/user/userLogin`, this.form).then((respose) => {
            if (respose.data.status === 200) {
              window.sessionStorage.setItem(
                "username",
                respose.data.data.username
              );
              window.sessionStorage.setItem("userId", respose.data.data.uid);
              if (respose.data.data.role === 1) this.$router.push("/admin/usermanage");
              else this.$router.push("/user/inoculate");
            } else {
              this.$message({
                type: "error",
                message: respose.data.message,
              });
              this.form.account = "";
              this.form.password = "";
            }
          });
        }
      });
    },
    res() {
      this.$router.push("/register");
    },
  },
};
</script>

<style scoped>
.main {
  width: 100%;
  height: 100vh;
  /* background-color: blueviolet; */
  margin: 0;
  background-image: url("../images/login1.jpg");
  /* background-size: 100%; */
  /* background-repeat: no-repeat; */
  overflow: hidden;
}
.container {
  width: 400px;
  margin: 150px auto;
}
h1 {
  text-align: center;
  color: white;
  font-size: 30px;
  padding: 30px 0;
}
.btn2 {
  margin-left: 70px;
  width: 30%;
}
.btn1 {
  margin-right: 70px;
  width: 30%;
}
</style>